package com.softserve.academy.food.controllers;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.softserve.academy.food.services.iOrderService;
import com.softserve.academy.food.services.supplement.Convector;

@Controller
@RequestMapping( value="/order" )
public class OrderController  
{
	@Autowired
	private iOrderService oService;
	
	@RequestMapping( params={"action=add"}, method = RequestMethod.POST )
	public String addBasked( HttpServletRequest request, HttpSession session )
	{
		setBaskedData( session, request );
		return "redirect:/index.htm";
	}
	
	@RequestMapping( params={"action=commit"}, method = RequestMethod.POST )
	public String comitBasked( HttpServletRequest request, HttpSession session )
	{
		if ( getLogin().equals("guest") )
		{
			return "redirect:/index.htm?action=errLogin";
		}
		return "redirect:/index.htm?action=commit";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping( params={"action=finish"}, method = RequestMethod.POST )
	public String finishOrder( HttpServletRequest request, HttpSession session )
	{
		if ( getLogin().equals("guest") )
		{
			return "redirect:/index.htm?action=errLogin";
		}
		
		ArrayList<Integer> baskedIdList = (ArrayList<Integer>)session.getAttribute("baskedIdList");
		if( !baskedIdList.isEmpty() )
		{
			oService.addOrder( baskedIdList , getLogin() );
			clearBasked( session, request );
			return "redirect:/index.htm?action=finish";
		}
		return "redirect:/index.htm";
	}
	
	@SuppressWarnings("unchecked")
	private void setBaskedData( HttpSession session,  HttpServletRequest request )
	{
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String id = request.getParameter("id");
		
		if ( session.getAttribute("baskedIdList")!=null )
		{
			ArrayList<Integer> baskedIdList = (ArrayList<Integer>)session.getAttribute("baskedIdList");
			ArrayList<String> baskedNameList = (ArrayList<String>)session.getAttribute("baskedNameList");
			float baskedSumm = (Float)session.getAttribute("baskedSumm");
			
			baskedIdList.add( Convector.StringToInt(id) );
			baskedNameList.add( name );
			baskedSumm+= Convector.StringToFloat(price);
			
			session.setAttribute("baskedIdList", baskedIdList );
			session.setAttribute("baskedNameList", baskedNameList );
			session.setAttribute("baskedSumm", baskedSumm );
		}
	}
	
	private void clearBasked( HttpSession session,  HttpServletRequest request )
	{
		if ( session.getAttribute("baskedIdList")!=null )
		{
			session.setAttribute("baskedNameList", new  ArrayList<String>());
			session.setAttribute("baskedSumm", new Float(0));
			session.setAttribute("baskedIdList", new ArrayList<Integer>() );
		}
	}
	
	
	private String getLogin()
	{
		String login;
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if ( principal instanceof UserDetails ) 
		{
			login = ( (UserDetails)principal ).getUsername();
		} 
		else 
		{
			login = principal.toString();
		}
		return login;
	}


}

